

package cn.liyao.web.controller;

import cn.liyao.tool.annotation.RequireToken;
import cn.liyao.tool.api.R;
import cn.liyao.web.constant.ConversationType;
import cn.liyao.web.holder.CurrentUserIdHolder;
import cn.liyao.web.model.*;
import cn.liyao.web.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.util.List;

@RestController
public class MessageController {

    private final EntityManager entityManager;


    private final UserRepository userRepository;

    private final MessageRepository messageRepository;

    private final OfflineMessageRepository offlineMessageRepository;

    private final ConversationRepository conversationRepository;

    @Autowired
    public MessageController(EntityManager entityManager, UserRepository userRepository, MessageRepository messageRepository, OfflineMessageRepository offlineMessageRepository, ConversationRepository conversationRepository) {
        this.entityManager = entityManager;
        this.userRepository = userRepository;
        this.messageRepository = messageRepository;
        this.offlineMessageRepository = offlineMessageRepository;
        this.conversationRepository = conversationRepository;
    }


    @GetMapping("/message/list")
    @RequireToken()
    @Transactional()
    public R massagePage(String targetId, Long timestamp) {
        String userId = CurrentUserIdHolder.getCurrentUser();
        if(timestamp == null){
            timestamp = System.currentTimeMillis();
        }
        if(targetId.indexOf("group_") == -1){
            Slice<Messages> list = messageRepository.findMessageListPrivate(targetId, userId,timestamp,targetId,PageRequest.of(0, 10));
            offlineMessageRepository.deleteAllByFrom(targetId);
            Conversation conversation =  conversationRepository.findOneByUserIdAndTargetId(userId,targetId);
            if(conversation != null){
                conversation.setUnreadCount(0);
                conversationRepository.save(conversation);
            }
            return R.data(list.getContent());
        }else{
            Slice<Messages> list = messageRepository.findGroupMessageList(targetId,timestamp,targetId,userId,PageRequest.of(0, 10));
            offlineMessageRepository.deleteAllByFromAndTo(targetId,userId);

            Conversation conversation =  conversationRepository.findOneByUserIdAndTargetId(userId,targetId);
            if(conversation != null){
                conversation.setUnreadCount(0);
                conversationRepository.save(conversation);
            }
            return R.data(list.getContent());
        }
    }


    @GetMapping("/message/offline")
    @RequireToken()
    public R getAllOfflineMessage() {
        String userId = CurrentUserIdHolder.getCurrentUser();
        List<OfflineMessages> offlineMessages = offlineMessageRepository.findAllByToAndType(userId, ConversationType.System.getValue());
        return R.data(offlineMessages);
    }

}